<template>
  <div class="page-height-list-wrapper" style="height:100%">
    <searchData
      :search-data="searchArr"
      @search="handleSearch"
    />

    <a-card :bordered="false"  class="page-height-list">
      <!-- <title-name :title="filterTitle()" /> -->
      <!-- 业主 -->
      <div
        v-if="userType === 'owner'"
        class="table-operator"
      >
        <a-button
          v-btnPermission="'sy_004_add_01'"
          type="primary"
          @click="handleAdd"
        >
          新增业主登录信息
        </a-button>
      </div>

      <div
        v-if="userType === 'owner'"
        class="pub-table-select-count"
      >
        <a-icon
          type="info-circle"
          theme="twoTone"
        />
        <span style="margin-right: 12px">
          {{ filterTitle('content') }}总数：<span class="totalNum">{{ totalDels.totalNum || 0 }} 个</span> | 可用：<span
            class="totalNum"
          >{{ totalDels.availableNum || 0 }} 个</span>
          | 不可用：<span class="totalNum">{{ totalDels.disabledNum || 0 }} 个</span>
        </span>
      </div>
      <!-- 分包商 -->
      <div
        v-if="userType === 'subcontractor'"
        class="pub-table-select-count"
      >
        <a-icon
          type="info-circle"
          theme="twoTone"
        />
        <span style="margin-right: 12px">
          {{ filterTitle('content') }}总数：<span class="totalNum">{{ totalDels.totalNum || 0 }} 个</span>
        </span>
      </div>

        <list-table
          ref="table"
          row-key="id"
          :columns="columns"
          :request="loadData"
          :alert="true"
        >
        <span
          slot="serial"
          slot-scope="text, record, index"
        >{{ index + 1 }}</span>
        <span
          slot="action"
          slot-scope="text, record, index"
        >
          <MoreButton>
          <template v-if="userType === 'owner'">
            <a
              v-btnPermission="'sy_004_loo_02'"
              @click="handleSub(record)"
            >查看</a>
            <a
              v-btnPermission="'sy_004_edi_03'"
              @click="handleEdit(record)"
            >编辑</a>
            <a
              v-btnPermission="'sy_004_res_04'"
              @click="resetPwd(record)"
            >重置密码</a>
            <a
              v-btnPermission="'sy_004_del_05'"
              @click="handleClick(record)"
            >删除</a>
          </template>
          <template v-if="userType === 'subcontractor'">
            <a
              v-btnPermission="'sy_005_loo_01'"
              @click="handleLoookSub(record)"
            >查看</a>
            <a
              v-btnPermission="'sy_005_res_02'"
              @click="resetPwd(record)"
            >重置密码</a>
          </template>
            </MoreButton>
        </span>
      </list-table>

      <!--修改密码-->
      <a-modal
        title="二次授权"
        :visible="visiblePwd"
        :confirm-loading="confirmLoading"
        destroy-on-close
        @ok="resetUserPwdReq"
        @cancel="handleCancelPwd"
      >
        <a-form-model
          ref="ruleForm"
          :model="formPwd"
          :rules="ruless"
          :label-col="{ lg: { span: 6 }, sm: { span: 6 } }"
          :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
        >
          <a-row :gutter="24">
            <a-col
              :md="24"
              :sm="24"
            >
              <a-form-model-item
                label="登录密码"
                prop="authPwd"
                class="requiredFormItem"
              >
                <a-input-password
                  v-model.trim="formPwd.authPwd"
                  placeholder="请输入登录密码"
                >
                  <a-icon
                    slot="prefix"
                    type="lock"
                    style="color: rgba(0, 0, 0, 0.25)"
                  />
                </a-input-password>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-modal>

      <!--新增账号-->
      <a-modal
        v-model="visible"
        width="800px"
        :title="title"
        destroy-on-close
      >
        <template slot="footer">
          <a-button
            key="back"
            @click="handleCancel"
          >
            取消
          </a-button>
          <a-button
            key="submit"
            :loading="loading"
            type="primary"
            @click="handleOk"
          >
            确定
          </a-button>
        </template>
        <a-form-model
          ref="form1"
          :model="form"
          :rules="rules"
        >
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="分公司"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="branchId"
              >
                <SelectSubCompany
                  v-model="form.branchId"
                  placeholder="请选择"
                  @change="clearData"
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="项目名称"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="projectId"
                class="requiredFormItem"
              >
                <SelectProject
                  v-model="form.projectId"
                  placeholder="请选择"
                  :is-clear="true"
                  :form="form"
                  key-value="branchId"
                  @projects="projectsData"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="项目编号"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="form.projectNum"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="建设单位名称"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="form.companyName"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="建设单位性质"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="form.natureName"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="建设单位统一社会信用代码"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="form.creditCode"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="登录人姓名"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="loginName"
                class="requiredFormItem"
              >
                <BaseInput
                  v-model="form.loginName"
                  placeholder="输入登录人姓名"
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="登录账号(电话号码)"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="loginAccount"
                class="requiredFormItem"
              >
                <BaseInput
                  v-model="form.loginAccount"
                  placeholder="输入登录人账号"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="建设单位项目负责人姓名"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="principalName"
                class="requiredFormItem"
              >
                <BaseInput
                  v-model="form.principalName"
                  placeholder="输入建设单位项目负责人姓名"
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="创建人"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="reasonCode"
              >
                <BaseInput
                  v-model="form.creatorName"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="创建时间"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="form.createTime"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="状态"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="enabled"
                class="requiredFormItem"
              >
                <a-select
                  v-model="form.enabled"
                  allow-clear
                  placeholder="请选择"
                >
                  <a-select-option :value="1">
                    可用
                  </a-select-option>
                  <a-select-option :value="0">
                    不可用
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-modal>

      <!--查看账号-->
      <a-modal
        v-model="visibles"
        width="800px"
        :title="'查看业主登录信息'"
      >
        <template slot="footer">
          <a-button
            key="back"
            @click="handleCancels"
          >
            关闭
          </a-button>
        </template>
        <a-form-model
          ref="form1"
          :model="form"
          :rules="rules"
        >
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="分公司"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="branchId"
              >
                <BaseInput
                  v-model="forms.branchName"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="项目名称"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="projectId"
              >
                <BaseInput
                  v-model="forms.projectName"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="项目编号"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="forms.projectNum"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="建设单位名称"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="forms.companyName"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="建设单位性质"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="forms.natureName"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="建设单位统一社会信用代码"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="forms.creditCode"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="登录人姓名"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="loginName"
              >
                <BaseInput
                  v-model="forms.loginName"
                  placeholder="输入登录人姓名"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="登录账号(电话号码)"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="loginAccount"
              >
                <BaseInput
                  v-model="forms.loginAccount"
                  placeholder="输入登录人账号"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="建设单位项目负责人姓名"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="principalName"
              >
                <BaseInput
                  v-model="forms.principalName"
                  placeholder="输入建设单位项目负责人姓名"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="创建人"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="reasonCode"
              >
                <BaseInput
                  v-model="forms.creatorName"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="创建时间"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput
                  v-model="forms.createTime"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="12"
              :sm="12"
            >
              <a-form-model-item
                label="状态"
                :label-col="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapper-col="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="enabled"
              >
                <BaseInput
                  v-model="forms.enabledName"
                  disabled
                />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-modal>
      <!-- 分包商查看详情 -->
      <SubContractorDetail
        :init-data="childInitData"
        :visible="visibleState.subContractor"
        @cancel="visibleState.subContractor = false"
      />
    </a-card>
  </div>
</template>

<script>
import { resetUserPwd } from '@/api/commons.js'
import { packageConfirm } from '@/utils/baseAction.js'
import { PublicKey } from '@/config'
import { encryptFromText } from '@/tool/sm.js'
import SubContractorDetail from './SubContractorDetail.vue'
import { getSubManagerList, deleteOwner } from '@/api/system/systemUserManage.js'
import { STable } from '@/components'
import { getLoginsList, getLoginTotal, addLogins, editLogins } from '@/api/project/loginInfo'
import { updataUserManagePsw } from '@/api/system/system'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/SelectProjects/SelectProject' // 项目
import ARow from 'ant-design-vue/es/grid/Row'
 import { isPhone, createValidator} from '@/utils/regExpression'
const searchArr = [
  {
    name: '业主名称',
    value: 'ownerName',
    type: 'input',
  },
  {
    name: '项目名称',
    value: 'projectName',
    type: 'input',
  },
]
const subSearchArr = [
  {
    name: '分包商名称',
    value: 'subContractorName',
    type: 'input',
  },
  {
    name: '项目名称',
    value: 'projectName',
    type: 'input',
  },
]

const columns = [
  {
    title: '序号',
    width: '65px',
    scopedSlots: { customRender: 'serial' },
  },
  {
    title: '建设单位名称(业主)',
    dataIndex: 'companyName',
  },
  {
    title: '建设单位性质',
    dataIndex: 'natureName',
  },
  {
    title: '建设单位统一社会信用代码',
    dataIndex: 'creditCode',
  },
  {
    title: '建设单位项目负责人姓名',
    dataIndex: 'principalName',
  },
  {
    title: '登录人姓名',
    dataIndex: 'loginName',
  },
  {
    title: '登录账号(电话号码)',
    dataIndex: 'loginAccount',
  },
  {
    title: '项目',
    dataIndex: 'projectName',
  },
  {
    title: '分公司',
    dataIndex: 'branchName',
  },
  {
    title: '项目编号',
    dataIndex: 'projectNum',
  },
  {
    title: '创建人',
    dataIndex: 'creatorName',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '状态',
    dataIndex: 'enabledName',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '200px',
    fixed: 'right',
    scopedSlots: { customRender: 'action' },
  },
]
const subColumns = [
  {
    title: '序号',
    width: '65px',
    scopedSlots: { customRender: 'serial' },
  },
  {
    title: '分包商名称',
    dataIndex: 'subContractorName',
  },
  {
    title: '登录人姓名',
    dataIndex: 'name'
  },
  {
    title: '登录账号（电话号码）',
    dataIndex: 'phone',
  },
  {
    title: '项目',
    dataIndex: 'projectName',
  },
  {
    title: '分公司',
    dataIndex: 'branchName',
  },
  {
    title: '项目编号',
    dataIndex: 'projectNum',
  },
  {
    title: '登录人员岗位',
    dataIndex: 'postName',
  },
  {
    title: '状态',
    dataIndex: 'statusName',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '200px',
    fixed: 'right',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  name: 'TableListS',
  components: {
    ARow,
    STable,
    SelectSubCompany,
    SelectProject,
    SubContractorDetail,
  },
  props: {
    // 用户类型，业主、分包商
    userType: {
      type: String,
      default: 'owner', // 'owner' | 'subcontractor'
    },
    searchQuery: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    const validatePass = (rule, value, callback) => {
      const reg = /([a-zA-Z0-9!@#$%^&*()_?<>{}]){6,18}/
      if (!reg.test(value)) {
        callback(new Error('请输入6到16位密码'))
      } else {
        callback()
      }
    }
    return {
      childInitData: {},
      visibleState: {
        subContractor: false,
      },
      columns: [],
      visiblePwd: false,
      confirmLoading: false,

      ruless: {
        authPwd: [{ required: true, message: '请输入登录密码', trigger: 'change' },
        { validator: validatePass, trigger: 'blur' }]
      },

      formPwd: {
        authPwd: '' // 二次授权密码
      },

      isShowPwd: 1,
      searchArr: [],

      loading: false,
      rules: {
        branchId: [{ required: true, message: '请选择分公司', trigger: 'change' }],
        projectId: [{ required: true, message: '请选择项目', trigger: 'change' }],
        loginName: [{ required: true, message: '请输入登录人姓名', trigger: 'blur' }],
        loginAccount: [{ required: true, message: '请输入登录账号(电话号码)', trigger: 'blur' },
        { validator: createValidator(isPhone, '手机号格式不正确', false), trigger: 'blur'}],
        loginPassword: [{ required: true, message: '请输入登录密码', trigger: 'blur' }],
        configloginPassword: [{ required: true, message: '请输入确认登录密码', trigger: 'blur' }],
        principalName: [{ required: true, message: '请输入建设单位项目负责人姓名', trigger: 'blur' }],
        enabled: [{ required: true, message: '请选择状态', trigger: 'blur' }],
      },
      visible: false,
      visibles: false,
      title: '',

      form: {
        projectId: undefined,
        loginPassword: '********',
        configloginPassword: '********',
      },

      forms: {},

      queryParam: {},

      data: [],
      selectedRowKeys: [],
      selectedRows: [],
      points: [],
      totalDels: {},

      modelPwd: {},

      ids: [], //删除ids
    }
  },

  created() {
    this.initPage()
    this.getLoginTotal()
  },
  methods: {
    /**
     * 重置密码
     */
    async resetUserPwdReq() {
      this.$refs.ruleForm.validate(async (valid) => {
        if(!valid) {
          return
        }
      try {
        if(this.confirmLoading) {
          return
        }
        this.confirmLoading = true
        const params = {
            sign: encryptFromText(this.childInitData.userId),
            pscode: this.doSm3AndSm2Encrypt(this.formPwd.authPwd),
            userId: this.childInitData.userId
        }
        await resetUserPwd(params)
        this.visiblePwd = false
        this.$message.success('重置成功')
        this.formPwd = {
          authPwd: ''
        }
      } catch(e) {
        console.error(e)
        e && this.$message.error(e.message || '请求失败，请稍后重试！')
      }
      this.confirmLoading = false
      })
    },
    handleClick(data) {
      packageConfirm({
        callback: async () => {
          const params = {
            ids: [data.id],
          }
          const res = await deleteOwner(params)
          this.$message.success('删除成功')
          this.handleSearch()
        },
      })
    },
    /**
     * 查看分包商用户信息详情
     */
    handleLoookSub(data) {
      this.childInitData = { ...data }
      this.visibleState.subContractor = true
    },
    loadData(parameter) {
      const requestParameters = Object.assign({}, parameter, this.queryParam)
      if (this.userType === 'owner') {
        return getLoginsList(requestParameters)
      } else {
        return getSubManagerList(requestParameters).then((res) => {
          this.totalDels = {
            totalNum: res.totalSize || 0,
          }
          return res
        })
      }
    },
    initPage() {
      // 业主用户和分包商用户分别显示不同列
      const columnMap = {
        owner: columns,
        subcontractor: subColumns,
      }
      this.columns = columnMap[this.userType] || []
      const searchMap = {
        owner: searchArr,
        subcontractor: subSearchArr,
      }
      this.searchArr = searchMap[this.userType] || []
    },
    filterTitle(type = 'all') {
      const map = {
        owner: '业主登录信息',
        subcontractor: '分包商管理人员登录信息',
      }
      if (type === 'content') {
        return map[this.userType] || ''
      }
      return (map[this.userType] && map[this.userType] + '列表') || ''
    },
    doSm3AndSm2Encrypt(sourceStr) {
      // 密码加密
      /* eslint-disable */
      const sm2Utils = new Sm2Utils()
      const public_key = PublicKey
      const sm3_random_plain = Sm3Utils.encryptFromText(sourceStr) + '|' + sm2Utils.randomWord(8) + '|' + sourceStr
      const sm3_sm2_plain = sm2Utils.encryptFromText(public_key, sm3_random_plain)
      return '{crypto}' + sm3_sm2_plain
      /* eslint-enable */
    },

    handleCancelPwd() {
      this.visiblePwd = false
      this.formPwd = {}
    },
    // 重置密码
    resetPwd(val) {
      this.visiblePwd = true
      this.modelPwd = val
      this.childInitData = { ...val }
    },
    getPwd(val) {
      if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(val)) {
        this.$message.error('密码必须由6-16位字母、数字组成，区分大小写！')
        return false
      }
    },
    configPwd(val) {
      if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(val.target.value)) {
        this.$message.error('密码必须由6-16位字母、数字组成，区分大小写！')
        return false
      }
      if (this.form.loginPassword != val.target.value) {
        this.$message.error('两次输入密码不一致，核对后请重新输入！')
        return
      }
    },
    // 获取项目信息
    projectsData(val) {
      this.form.projectNum = val.projectNum
      this.form.companyName = val.constructionUnitName
      this.form.natureName = val.constructionUnitNature
      this.form.creditCode = val.constructionUnitCreditCode
    },

    // 清空选中信息
    clearData() {
      this.form.projectNum = ''
      this.form.companyName = ''
      this.form.natureName = ''
      this.form.creditCode = ''
    },

    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },

    // 搜索
    handleSearch(val) {
      this.queryParam = {
        ...val
      }
      this.$refs.table.refresh(true)
      this.getLoginTotal()
    },

    // 统计
    getLoginTotal() {
      if (this.userType === 'owner') {
        getLoginTotal(this.queryParam).then((res) => {
          this.totalDels = res.data
        })
      }
    },

    handleAdd() {
      this.title = '新增业主登录信息'
      this.form = {}
      this.isShowPwd = 1
      this.form.creatorName = JSON.parse(sessionStorage.getItem('ycloud-user_info')).name
      this.form.createTime = this.getNowDate()
      this.form.loginPassword = 'CscecXn*2020'
      this.form.configloginPassword = 'CscecXn*2020'

      this.visible = true
    },

    // 查看
    handleSub(record) {
      this.isShowPwd = 0
      this.visibles = true
      this.forms = { ...record }
      this.forms.configloginPassword = record.loginPassword
    },

    // 编辑
    handleEdit(record) {
      this.isShowPwd = 0
      this.title = '编辑业主登录信息'
      this.visible = true
      this.$nextTick(() => {
        this.form.projectId = record.projectId
      })
      this.form = { ...record }
      this.form.configloginPassword = record.loginPassword
    },

    // 关闭弹层
    handleCancel() {
      this.visible = false
    },
    // 关闭弹层
    handleCancels() {
      this.visibles = false
    },

    // 确认业主登录信息
    handleOk() {
      let that = this
      if (that.loading) return

      that.$refs.form1.validate((err, value) => {
        if (err) {
          if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(that.form.loginAccount)) {
            that.$message.error('电话号码有误，核对后请重新输入')
            return false
          }

          if (that.form.id) {
            that.loading = true
            that.form.loginPassword = null
            editLogins(that.form)
              .then((res) => {
                that.loading = false
                if (res.code == 200 || res.data.code == 200) {
                  that.$message.success('编辑成功')
                  that.visible = false
                  that.$refs.table.refresh(true)
                  that.getLoginTotal()
                }
              })
              .catch(() => {
                that.loading = false
              })
          } else {
            if (that.form.loginPassword != that.form.configloginPassword) {
              that.$message.error('请确认两次密码是否一致，核对后请重新输入！')
              return
            }

            that.loading = true
            addLogins(that.form)
              .then((res) => {
                that.loading = false
                if (res.code == 200 || res.data.code == 200) {
                  that.$message.success('新增成功')
                  that.visible = false
                  that.$refs.table.refresh(true)
                  that.getLoginTotal()
                }
              })
              .catch(() => {
                that.loading = false
              })
          }
        }
      })
    },

    // 获取扣分分值对象值
    getExtraData(val) {
      const target = this.points.find((item) => item.deductScoreId === val)
      this.form.deductScore = target.deductScoreCode
      this.form.deductScoreName = target.deductScore
      this.form.status = target.status
    },
  },
}
</script>
<style lang="less" scoped>
@deep: ~'>>>';
@{deep} .ant-table-fixed {
  margin: 0;
}

::v-deep {
  .list-table .ant-table-small.ant-table-bordered .ant-table-fixed-right {
    border-left: 1px solid #e8e8e8;
  }
}
</style>



